package com.wjpeng.wms.service.impl;

import com.wjpeng.wms.domain.Client;
import com.wjpeng.wms.mapper.ClientMapper;
import com.wjpeng.wms.query.QueryObject;
import com.wjpeng.wms.service.IClientService;
import com.wjpeng.wms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ClientServiceImpl implements IClientService {
    @Autowired
    private ClientMapper clientMapper;

    @Override
    public void saveOrUpdate(Client d) {
        if(d.getId() == null){
            clientMapper.insert(d);
        }else{
            clientMapper.updateByPrimaryKey(d);
        }
    }

    @Override
    public void delete(Long id) {
            clientMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Client get(Long id) {
        return clientMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Client> list() {
        return clientMapper.selectAll();
    }

    @Override
    public PageResult result(QueryObject qo) {
        Integer count = clientMapper.queryCount(qo);
        if(count == 0){
            return new PageResult(Collections.EMPTY_LIST,0,1,0);
        }
        return new PageResult(clientMapper.queryLimit(qo),count,qo.getCurrentPage(),qo.getPageSize());
    }
}
